<template>
  <div class="scrap-dialog dialog">
    <basic-dialog
      :visible="open"
      minWidth="700px"
      :title="`${$attrs.toolData.msg}报废`"
      :isFullScreen="false"
      :isShowInnerLoading="isShowInnerLoading"
      @close="$emit('close')"
    >
      <q-form @submit="submit" class="dialog-content padding-20">
        <detail-dialog
          :headList="headList"
          :detailData="detailData"
          class="font-size-14"
        />
        <q-separator class="qa-line"/>
        <div class="" style="margin: 10px -5px 10px -15px;">
          <div class="">
            <yz-input
              inputType="textarea"
              v-model="queryParams.reason"
              title="报废原因"
              :maxlength="100"
            />
          </div>
          <div>
            <upload-picture
              v-model="queryParams.attachments"
              :max="9"
              title="上传附件"
              imgPdf
              ref="imgUpload"
              @uploadImg="uploadImg"
            />
          </div>
        </div>
        <q-separator class="qa-line"/>
        <div class="dialog-footer text-center margin-t-10">
<!--          <div class="absolute">-->
<!--            <q-checkbox left-label v-model="queryParams.isApproval" label="是否审核:" />-->
<!--          </div>-->
          <q-btn outline label="取消" color="primary" @click="$emit('close')" class="q-mr-md"/>
          <q-btn unelevated label="确认" color="primary" type="submit"/>
        </div>
      </q-form>
    </basic-dialog>
  </div>
</template>

<script>
import _ from 'lodash'
import { getScrapMold } from 'src/boot/tool/tool-code'

export default {
  name: 'scrap-dialog',
  props: {
    open: {
      type: Boolean,
      default: true,
      required: true
    },
    queryList: {
      type: Object
    }
  },
  components: {},
  data () {
    return {
      headList: {
        serialNumber: `${this.$attrs.toolData.msg}识别码`,
        model: `${this.$attrs.toolData.msg}型号`,
        name: `${this.$attrs.toolData.msg}名称`,
        type: `${this.$attrs.toolData.msg}类型`,
        spec: `${this.$attrs.toolData.msg}规格`,
        unit: '单位'
      },
      detailData: {},
      isShowInnerLoading: false,
      queryParams: {
        reason: '',
        attachments: [],
        isApproval: false // 是否审核[1:是, 2:否]
      }
    }
  },
  computed: {
    // 显示标题
  },
  watch: {
    open (newPro) {
      newPro && this.init()
    }
  },
  mounted () {},
  methods: {
    init () {
      this.detailData = _.cloneDeep(this.queryList)
      console.log(this.detailData.type)
      console.log(this.detailData.unit)
      Object.assign(this.queryParams, this.$options.data.call(this).queryParams)
      this.getDetail()
    },
    // 上传图片 在调用结束事件
    setM_Img () {
      if (!this.queryParams.attachments.length) {
        this.uploadImg(this.queryParams.attachments)
      } else {
        this.queryParams.attachments.map(v => this.$refs.imgUpload.uploadFlie(v))
      }
    },
    uploadImg (val) {
      this.queryParams.attachments = val
      this.setForm()
    },
    // 提交表单
    submit () {
      try {
        this.setM_Img()
      } catch (err) {
        console.log(err)
      }
    },
    setForm () {
      const params = {
        moldId: this.queryList.id,
        ...this.queryParams
      }
      this.isShowInnerLoading = true
      params.isApproval = this.queryParams.isApproval ? 1 : 2
      getScrapMold(params).then(res => {
        this.isShowInnerLoading = false
        this.$message.success('报废成功')
        this.$emit('close')
      }).catch(err => {
        this.isShowInnerLoading = false
        console.log(err)
      })
    }
    // getDetail () {
    //   this.isShowInnerLoading = true
    //   getGoodsById(this.queryList.goodsId).then(res => {
    //     this.isShowInnerLoading = false
    //   }).catch(err => {
    //     this.isShowInnerLoading = false
    //     console.log(err)
    //   })
    // }
  }
}
</script>

<style scoped lang="scss">
</style>
